<?php
function unescape($str) {
    $str = rawurldecode($str);
    preg_match_all("/(?:%u.{4})|&#x.{4};|&#\d+;|.+/U",$str,$r);
    $ar = $r[0];
    //print_r($ar);
    foreach($ar as $k=>$v) {
        if(substr($v,0,2) == "%u"){
            $ar[$k] = iconv("UCS-2BE","UTF-8",pack("H4",substr($v,-4)));
  }
        elseif(substr($v,0,3) == "&#x"){
            $ar[$k] = iconv("UCS-2BE","UTF-8",pack("H4",substr($v,3,-1)));
  }
        elseif(substr($v,0,2) == "&#") {
            
            $ar[$k] = iconv("UCS-2BE","UTF-8",pack("n",substr($v,2,-1)));
        }
    }
    return join("",$ar);
}

if($mc->get($fromUsername . "_do")=="搜书"&&strtolower($form_Content)=="n")
{
 $page_number=$mc->get($fromUsername ."page_number");
 $now_page=$mc->get($fromUsername ."now_page")+1;
 $title=$mc->get($fromUsername ."title");
 $description=$mc->get($fromUsername ."description");
 $link= $mc->get($fromUsername ."link");
 $book_number=$mc->get($fromUsername ."book_number");
    if($now_page<$page_number)
    {
         $resultStr = "<xml>
           <ToUserName><![CDATA[$fromUsername]]></ToUserName>
           <FromUserName><![CDATA[$toUsername]]></FromUserName>
           <CreateTime>".time()."</CreateTime>
           <MsgType><![CDATA[news]]></MsgType>
           <ArticleCount>7</ArticleCount>
           <Articles>
           <item>
           <Title><![CDATA[共有".$book_number."条搜索记录]]></Title> 
           <Description><![CDATA[]]></Description>
           <PicUrl><![CDATA[http://b164.photo.store.qq.com/psb?/V12Zu6dj4Udk58/Wlh*bNggwMA32Q26TKqEnRPU824Zlakis6m65JuOK0o!/b/dOX0wWEWMAAA&bo=IAN9AgAAAAABAHs!&rf=photoDetail]]></PicUrl>
           <Url><![CDATA[]]></Url>
           </item> ";
          
          
      for($i=($now_page-1)*5+1;$i<$now_page*5+1;$i++)
      {      $newtitle=unescape($title[1][$i]);
             $newdescript=unescape($description[1][$i]);
      
          $newdescript=strstr($newdescript, '出版信息:');
          $text=$newtitle."\n  ".$newdescript;
           
           $link_book=$link[1][$i-1];
         $link_book=strstr($link_book,'marc_no');
          $count=strpos($link_book,"marc_no="); 
           $id=substr_replace($link_book,"",$count,8); 
           $url="http://202.116.41.246/m//opac/detail.action?id=".$id."&weixinCode=".$fromUsername;
            $resultStr.="
           <item>
           <Title><![CDATA[".$text."]]></Title> 
           <Description><![CDATA[]]></Description>
           <PicUrl><![CDATA[]]></PicUrl>
           <Url><![CDATA[$url]]></Url>
           </item>
           ";
       }
            
            $resultStr.="  
            <item>
           <Title><![CDATA[回复【n】下一页\n回复【菜单】弹出功能菜单\n回复其他从新搜索]]></Title> 
           <Description><![CDATA[]]></Description>
           <PicUrl><![CDATA[]]></PicUrl>
           <Url><![CDATA[]]></Url>
           </item> 
           </Articles>
           <FuncFlag>1</FuncFlag>
           </xml> ";
  echo $resultStr;
  $mc->set($fromUsername . "_do", "搜书", 0, 1000);
  $mc->set($fromUsername ."page_number",$page_number,0,1000);
  $mc->set($fromUsername ."now_page",$now_page,0,1000);
  $mc->set($fromUsername ."title",$title,0,1000);
  $mc->set($fromUsername ."description",$description,0,1000);
  $mc->set($fromUsername ."link",$link,0,1000);
  $mc->set($fromUsername ."book_number",$book_number,0,1000);
  exit;
        
        
    }
    if($now_page==$page_number)
    {
        $ArticleCount=$book_number-($now_page-1)*5+2;
         $resultStr = "<xml>
           <ToUserName><![CDATA[$fromUsername]]></ToUserName>
           <FromUserName><![CDATA[$toUsername]]></FromUserName>
           <CreateTime>".time()."</CreateTime>
           <MsgType><![CDATA[news]]></MsgType>
           <ArticleCount>$ArticleCount</ArticleCount>
           <Articles>
           <item>
           <Title><![CDATA[共有".$book_number."条搜索记录]]></Title> 
           <Description><![CDATA[]]></Description>
           <PicUrl><![CDATA[http://b164.photo.store.qq.com/psb?/V12Zu6dj4Udk58/Wlh*bNggwMA32Q26TKqEnRPU824Zlakis6m65JuOK0o!/b/dOX0wWEWMAAA&bo=IAN9AgAAAAABAHs!&rf=photoDetail]]></PicUrl>
           <Url><![CDATA[]]></Url>
           </item> ";
          
          $ArticleCount-=1;
      for($i=($now_page-1)*5+1;$i<=$book_number;$i++)
      {      $newtitle=unescape($title[1][$i]);
             $newdescript=unescape($description[1][$i]);
           
          $newdescript=strstr($newdescript, '出版信息:');
          $text=$newtitle."\n  ".$newdescript;
          
           $link_book=$link[1][$i-1];
         $link_book=strstr($link_book,'marc_no');
          $count=strpos($link_book,"marc_no="); 
           $id=substr_replace($link_book,"",$count,8); 
           $url="http://202.116.41.246/m//opac/detail.action?id=".$id."&weixinCode=".$fromUsername;
           
            $resultStr.="
           <item>
           <Title><![CDATA[".$text."]]></Title> 
           <Description><![CDATA[]]></Description>
           <PicUrl><![CDATA[]]></PicUrl>
           <Url><![CDATA[$url]]></Url>
           </item>
           ";
       }
            
            $resultStr.="  
            <item>
           <Title><![CDATA[这已经是最后一页了\n回复【菜单】弹出功能菜单\n回复其他从新搜索]]></Title> 
           <Description><![CDATA[]]></Description>
           <PicUrl><![CDATA[]]></PicUrl>
           <Url><![CDATA[]]></Url>
           </item> 
           </Articles>
           <FuncFlag>1</FuncFlag>
           </xml> ";
  
           echo $resultStr;
           $mc->set($fromUsername . "_do", "搜书", 0, 1000);
           $mc->set($fromUsername ."page_number",$page_number,0,1000);
           $mc->set($fromUsername ."now_page",$now_page,0,1000);
           exit;
        
    }
     if($now_page>$page_number)
    {
    $contentStr="对不起，上面已经是最后一页了\n回复书名从新搜索\n回复【菜单】弹出功能菜单";
    $resultStr = sprintf($textTpl, $fromUsername, $toUsername,"text", $contentStr);
    echo $resultStr;
    $mc->set($fromUsername . "_do", "搜书", 0, 1000);
    exit;
        
    }
    
    
}

if($mc->get($fromUsername . "_do")=="搜书")
{ 
  $title=$form_Content;
  $url="http://202.116.41.246:8080/opac/search_rss.php?dept=ALL&title=".$title."&doctype=ALL&lang_code=ALL&match_flag=forward&displaypg=20&showmode=list&orderby=DESC&sort=CATA_DATE&onlylendable=yes";
  $f = new SaeFetchurl();
  $library = $f->fetch($url);
  if($library==false)
  { $contentStr="对不起查询失败。亲请从新搜索";
    $mc->set($fromUsername . "_do", "搜书", 0, 1000);
    $resultStr = sprintf($textTpl, $fromUsername, $toUsername,"text", $contentStr);
    echo $resultStr;
    exit;
      
  }
  preg_match_all( "/\<title\>(.*?)\<\/title\>/", $library, $title);
  preg_match_all( "/\<description\>(.*?)\<\/description\>/", $library, $description);
  preg_match_all( "/\<link\>(.*?)\<\/link\>/", $library, $link);
  $book_number=count($title[1])-1;
  $page_number= floor(($book_number+4)/5 );  
 
  if($book_number==0)
  {
    $contentStr="对不起，该书数据为0，请从新搜索";
    $mc->set($fromUsername . "_do", "搜书", 0, 1000);
    $resultStr = sprintf($textTpl, $fromUsername, $toUsername,"text", $contentStr);
    echo $resultStr;
    exit;
      
  }
    
    
    if($book_number<5)
        $ArticleCount=$book_number+2;
    else 
        $ArticleCount=7;
    
           $resultStr = "<xml>
           <ToUserName><![CDATA[$fromUsername]]></ToUserName>
           <FromUserName><![CDATA[$toUsername]]></FromUserName>
           <CreateTime>".time()."</CreateTime>
           <MsgType><![CDATA[news]]></MsgType>
           <ArticleCount>$ArticleCount</ArticleCount>
           <Articles>
           <item>
           <Title><![CDATA[共有".$book_number."条搜索记录]]></Title> 
           <Description><![CDATA[]]></Description>
           <PicUrl><![CDATA[http://b164.photo.store.qq.com/psb?/V12Zu6dj4Udk58/Wlh*bNggwMA32Q26TKqEnRPU824Zlakis6m65JuOK0o!/b/dOX0wWEWMAAA&bo=IAN9AgAAAAABAHs!&rf=photoDetail]]></PicUrl>
           <Url><![CDATA[]]></Url>
           </item> ";
          
          $ArticleCount-=1;
      for($i=1;$i< $ArticleCount;$i++)
      {      $newtitle=unescape($title[1][$i]);
             $newdescript=unescape($description[1][$i]);
             $newdescript=strstr($newdescript, '出版信息:');
          $text=$newtitle."\n  ".$newdescript;
          $link_book=$link[1][$i-1];
         $link_book=strstr($link_book,'marc_no');
          $count=strpos($link_book,"marc_no="); 
           $id=substr_replace($link_book,"",$count,8); 
           $url="http://202.116.41.246/m//opac/detail.action?id=".$id."&weixinCode=".$fromUsername;
            $resultStr.="
           <item>
           <Title><![CDATA[".$text."]]></Title> 
           <Description><![CDATA[]]></Description>
           <PicUrl><![CDATA[]]></PicUrl>
           <Url><![CDATA[$url]]></Url>
           </item>
           ";
       }
            
            $resultStr.="  
            <item>
           <Title><![CDATA[回复【n】下一页\n回复【菜单】弹出功能菜单\n回复其他从新搜索]]></Title> 
           <Description><![CDATA[]]></Description>
           <PicUrl><![CDATA[]]></PicUrl>
           <Url><![CDATA[]]></Url>
           </item> 
           </Articles>
           <FuncFlag>1</FuncFlag>
           </xml> ";
  
  echo $resultStr;
  $mc->set($fromUsername ."page_number",$page_number,0,1000);
  $mc->set($fromUsername ."now_page",1,0,1000);
  $mc->set($fromUsername ."title",$title,0,1000);
  $mc->set($fromUsername ."description",$description,0,1000);
  $mc->set($fromUsername ."link",$link,0,1000);
  $mc->set($fromUsername ."book_number",$book_number,0,1000);
  exit;
}

 
 
 if($form_Content="搜书")
 {
    $contentStr="欢迎进入搜书模式,直接输入书名搜索";
    $mc->set($fromUsername . "_do", "搜书", 0, 1000);
    $resultStr = sprintf($textTpl, $fromUsername, $toUsername,"text", $contentStr);
    echo $resultStr;
    exit;
 }
 
 
 
?>